package com.download.web.mapper;

import com.download.web.entity.BusArticle;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.download.web.entity.model.ArticleListModel;
import com.download.web.entity.model.ArticleModel;
import org.apache.ibatis.annotations.*;
import org.mybatis.spring.annotation.MapperScan;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author cmy
 * @since 2019-02-20
 */
@Mapper
@MapperScan
public interface BusArticleMapper extends BaseMapper<BusArticle> {

    /**
     * 分页查找所有文章列表
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Select(value = "select a.id,a.create_time,a.browse_num,a.app_name,a.title,a.visible_type,i.`name` typeName," +
            "im.path img " +
            "from bus_article a " +
            "LEFT JOIN sys_nav_bar i on a.type=i.id " +
            "LEFT JOIN bus_images im on a.id=im.article_id " +
            "LIMIT ${pageIndex},${pageSize} ")
    List<ArticleListModel> articlePage(@Param("pageIndex") Integer pageIndex,@Param("pageSize") Integer pageSize);

    /**
     * 按是否轮播图片分页查找文章
     * @param pageIndex
     * @param pageSize
     * @param isRotation
     * @return
     */
    @Select(value = "select a.id,a.create_time,a.browse_num,a.app_name,a.title,a.visible_type,i.`name` typeName,a.under_type as underType , " +
            "im.path img " +
            "from bus_article a " +
            "LEFT JOIN sys_nav_bar i on a.under_type=i.id " +
            "LEFT JOIN bus_images im on a.id=im.article_id " +
            " where is_rotation = #{isRotation} " +
            " order by a.create_time desc " +
            "LIMIT ${pageIndex},${pageSize} ")
    List<ArticleListModel> articlePageByIsRotation(@Param("pageIndex") Integer pageIndex,@Param("pageSize") Integer pageSize,@Param("isRotation")Integer isRotation);

    /**
     * 分页按文章类别查找文章列表
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Select(value = "select a.id,a.create_time,a.browse_num,a.app_name,a.content,a.title,a.visible_type,i.`name` typeName,a.under_type as underType ," +
            "im.path img " +
            "from bus_article a " +
            "LEFT JOIN sys_nav_bar i on a.under_type=i.id " +
            "LEFT JOIN bus_images im on a.id=im.article_id " +
            "where category=${category} " +
            "LIMIT ${pageIndex},${pageSize} ")
    List<ArticleListModel> articlePageByCategory(@Param("pageIndex") Integer pageIndex,@Param("pageSize") Integer pageSize,@Param("category") Integer category);

    @Select(value = "select count(1) " +
            "from bus_article a " +
            "LEFT JOIN bus_dic_item i on a.type=i.id " +
            "LEFT JOIN bus_images im on a.id=im.article_id " )
    int articleCount();

    @Select(value = "select count(1) " +
            "from bus_article a " +
            "LEFT JOIN bus_dic_item i on a.type=i.id " +
            "LEFT JOIN bus_images im on a.id=im.article_id " +
            "where is_rotation = #{isRotation} ")
    int articleCountByIsRotation(@Param("isRotation")Integer isRotation);

    @Insert(value = " insert into bus_article(content,create_time,browse_num,name,title,type,visible_type) " +
            " values(#{articleModel.content},#{articleModel.createTime},${articleModel.browseNum}," +
            " #{articleModel.title},${articleModel.type},${articleModel.visibleType}) ")
    int insertArticle(@Param("articleModel") ArticleModel articleModel);

    @Select(value = "select a.*,n.`name` typeName ,i.path appImg from bus_article a    " +
            "            LEFT JOIN sys_nav_bar n on n.id=a.under_type " +
            " left join bus_images i on i.article_id=a.id     " +
            "where a.id=${id}")
    ArticleModel findArticleInfoById(@Param("id") Integer id);

    /**
     * 按文章小分类查找文章
     * @param pageIndex
     * @param pageSize
     * @param typeId
     * @return
     */
    @Select(value = "select a.id,a.create_time,a.browse_num,a.app_name,a.title,a.visible_type,i.`name` typeName," +
            "im.path img " +
            "from bus_article a " +
            "LEFT JOIN sys_nav_bar i on a.under_type=i.id " +
            "LEFT JOIN bus_images im on a.id=im.article_id " +
            "where under_type=${typeId} or type=${typeId} " +
            "LIMIT ${pageIndex},${pageSize} ")
    List<ArticleListModel> articlePageByTypeId(@Param("pageIndex") Integer pageIndex,@Param("pageSize") Integer pageSize,@Param("typeId") Integer typeId);
    @Select(value = "select count(1) " +
            "from bus_article a " +
            "LEFT JOIN bus_dic_item i on a.type=i.id " +
            "LEFT JOIN bus_images im on a.id=im.article_id " +
            "where under_type=${typeId} or type=${typeId} ")
    int articleCountByType(@Param("typeId") Integer typeId);


    List<ArticleListModel> articlePageByTypeAndKeywords(@Param("pageIndex") Integer pageIndex,@Param("pageSize") Integer pageSize,
                                                        @Param("typeId") Integer typeId,@Param("keywords") String keywords);

    int articleCountByTypeAndKeywords(@Param("typeId") Integer typeId,@Param("keywords") String keywords);


    @Select("select count(1)   " +
            "            from bus_article a    " +
            "LEFT JOIN sys_nav_bar i on a.under_type=i.id " +
            "LEFT JOIN bus_images im on a.id=im.article_id " +
            "            where a.title like '%${keyword}%'  " )
    int articleCountByKeyword(@Param("keyword") String keyword);

    Integer articleCountByKeyword1();

    @Select("select a.id,a.create_time,a.browse_num,a.app_name,a.title,a.visible_type,i.`name` typeName,a.under_type as underType , " +
            "im.path img " +
            "from bus_article a " +
            "LEFT JOIN sys_nav_bar i on a.under_type=i.id " +
            "LEFT JOIN bus_images im on a.id=im.article_id " +
            "                where a.title like '%${keyword}%'  " +
            "             order by a.create_time desc    " +
            "            LIMIT ${pageIndex},${pageSize} ")
    List<ArticleListModel> articlePageByKeyword(@Param("pageIndex") Integer pageIndex,@Param("pageSize") Integer pageSize,@Param("keyword") String keyword);


    /**
     * 按文章标签查找文章列表
     * @param pageIndex
     * @param pageSize
     * @param category
     * @return
     */
    @Select(value = "select a.id,a.create_time,a.browse_num,a.app_name,a.content,a.title,a.visible_type,i.`name` typeName,a.under_type as underType ," +
            "im.path img " +
            "from bus_article a " +
            "LEFT JOIN sys_nav_bar i on a.under_type=i.id " +
            "LEFT JOIN bus_images im on a.id=im.article_id " +
            "LEFT JOIN bus_article_lable l on a.id=l.article_id  " +
//            "where category=${category} and l.lable_id=#{lable} " +
            "where l.lable_id=#{lable} " +
            "LIMIT ${pageIndex},${pageSize} ")
    List<ArticleListModel> articlePageByTags(@Param("pageIndex") Integer pageIndex,@Param("pageSize") Integer pageSize,@Param("category") Integer category,@Param("lable")String lable);

    @Select(value = "select count(1) " +
            "from bus_article a " +
            "LEFT JOIN bus_dic_item i on a.type=i.id " +
            "LEFT JOIN bus_images im on a.id=im.article_id "+
            "LEFT JOIN bus_article_lable l on a.id=l.article_id  " +
//            "where category=${category} and l.lable_id=#{lable} "
            "where l.lable_id=#{lable} ")
    int articleByTagsCount(@Param("category") Integer category,@Param("lable") String lable);

    @Select("select a.* from bus_article a where a.id in (SELECT article_id FROM bus_article_lable where lable_id=#{tags})  order by a.create_time  limit 0,1 ")
    BusArticle oneArtByTags(@Param("tags") String tags);

    @Update("update bus_article set browse_num=browse_num+1 where id=#{id} ")
    void browseNumAdd(@Param("id") Integer id);

}
